<?php
/**
 * Dao of Promotion
 *
 * @author fsqijy
 */
require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';

class PromotionDao extends BaseDao {

    protected $_name;

    protected $promotionConditions;
    public function  _setup() {
        $this->_name = $this->prefix() . "promotions";
        $this->promotionConditions = $this->prefix() . "promotion_conditions";
        parent::_setup();
    }

    public function getPromotionByType($type) {
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where('promotion_type = ?', $type);
        $select->where('is_delete = 0');
        $sql = $select->__toString();
        $result = $this->db_r->fetchRow($sql);
        return $result;
    }

    public function getFreeShippingPromotion() {
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->join($this->promotionConditions,$this->promotionConditions. ".promotion_id = ". $this->_name . ".id" ,"*");
        $select->where($this->_name . '.promotion_type = 1');
        $select->where($this->_name . '.is_delete = 0');
        $sql = $select->__toString();
        $result = $this->db_r->fetchRow($sql);
        return $result;
    }

    public function updateFreeShippingPromotion($promotion) {
        $where = $this->db_w->quoteInto('promotion_type = 1');
        $this->updateData($promotion,$where);
    }
    public function getPromotionByid($id) {
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where('id = ?', $id);
        $select->where('is_delete = 0');
        $sql = $select->__toString();
        $result = $this->db_r->fetchRow($sql);
        return $result;
    }
    public function updatePromotion($promotionid,$promotion) {
        $where = $this->db_w->quoteInto('id = ?', $promotionid);
        $this->updateData($promotion,$where);
    }
    public function deletePromotion($promotionid) {
        $data = array('is_delete' => 1);
        $where = $this->db_w->quoteInto('id = ?',$promotionid);
        $this->updateData($data, $where);
    }
    public function getPromotionByName($name) {
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where('lower(name) = ?', $name);
        $select->where('is_delete = 0');
        $sql = $select->__toString();
        $result = $this->db_r->fetchRow($sql);
        return $result;
    }
    public function getPromotionByCode($code) {
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where('lower(code) = ?', $code);
        $select->where('is_delete = 0');
        $sql = $select->__toString();
        $result = $this->db_r->fetchRow($sql);
        return $result;
    }

        /**
         * for implement promotion
         */
    public function getPromotionByCouponCode($code) {
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where('lower(code) = ?', $code);
        $select->where("date_from <= now()");
        $select->where("date_to >= now()");
        $select->where('status = 1');
        $select->where('is_delete = 0');
        $sql = $select->__toString();
        $result = $this->db_r->fetchRow($sql);
        return $result;
    }

}
